# -*- coding: utf-8 -*-
from db.mysql import MysqlClient
import requests
from common.helper import Helper
from common.config import *
from itertools import product


class Spider(object):
    def __init__(self):
        self.mysql = MysqlClient()
        self.name = "乐享阳光交通意外保障方案"

    def genProductInfo(self):
        plan = {
            "days": ["7d"],
            "people_count": ["1"],
            "airplane_death": ["0", "2000000"],
            "train_death": ["0", "2000000"],
            "passenger_death": ["0", "200000"],
            "myselfDriver": ["0", "200000"]
        }
        for v in product(plan["days"], plan["people_count"], plan["airplane_death"], plan["train_death"],
                         plan["passenger_death"], plan["myselfDriver"]):
            data = Helper.formatProductInfo(plan, v)
            yield data

    # 计算价格
    def calcPremium(self):
        maps = {'1000': 'QP000017,2', '0100': 'QP000018,5', '0010': 'QP000021,2', '0001': 'QP000020,2',
               '1100': 'QP000022,7',
               '1010': 'QP000024,4', '1001': 'QP000023,4', '0110': 'QP000027,7', '0101': 'QP000026,7',
               '0011': 'QP000028,4',
               '1110': 'QP000030,9', '1101': 'QP000029,9', '1011': 'QP000031,6', '0111': 'QP000032,9',
               '1111': 'QP000033,11'}

        for x in self.genProductInfo():

            selectCode = ""
            if x["airplane_death"] == "0":
                selectCode += "0"
            else:
                selectCode += "1"
            if x["train_death"] == "0":
                selectCode += "0"
            else:
                selectCode += "1"
            if x["passenger_death"] == "0":
                selectCode += "0"
            else:
                selectCode += "1"
            if x["myselfDriver"] == "0":
                selectCode += "0"
            else:
                selectCode += "1"
            if selectCode == "0000":
                continue

            price = Helper.decimalParse( maps[ selectCode ].split(",")[1] )
            data = {
                "name":self.name,
                "company":"阳光保险集团",
                "price":price
            }
            data.update( x )
            Helper.mergeTable( self.name,self.mysql,data )
